﻿using D.Wx.Dtos;
using D.Wx.Entities;
using FreeSql;

namespace D.Wx.IRepository
{
    public class SysPlatformRepository : ISysPlatformRepository
    {
        private readonly IUnitOfWork _uow;
        private readonly IFreeSql _freeSql;
        public SysPlatformRepository(IFreeSql freeSql, IUnitOfWork uow)
        {
            _freeSql = freeSql;
            _uow = uow;
        }
        public async Task<int> Add(SysPlatformEntity model, SysDwzEntity sysDwzEntity)
        {
            var res = 0;
            try
            {
                using var tran = _uow.GetOrBeginTransaction();

                await _freeSql.Insert<SysPlatformEntity>(model).WithTransaction(tran).ExecuteAffrowsAsync();
                if (sysDwzEntity!=null)
                {
                    await _freeSql.Insert<SysDwzEntity>(sysDwzEntity).WithTransaction(tran).ExecuteAffrowsAsync();
                }

                _uow.Commit();
                res++;
            }
            catch (Exception ex)
            {
                res = 0;
                _uow.Rollback();
                throw new Exception(ex.Message);
            }
            finally
            {
                _uow.Dispose();
            }
            return res;
        }

        public async Task<int> Delete(List<string> ids)
        {
            var res = 0;
            try
            {
                using var tran = _uow.GetOrBeginTransaction();

                await _freeSql.Delete<SysPlatformEntity>().Where(m => ids.Contains(m.Id)).WithTransaction(tran).ExecuteAffrowsAsync();
                await _freeSql.Delete<SysDwzEntity>().Where(m => ids.Contains(m.SysPlatformId)).WithTransaction(tran).ExecuteAffrowsAsync();

                _uow.Commit();
                res++;
            }
            catch (Exception ex)
            {
                res = 0;
                _uow.Rollback();
                throw new Exception(ex.Message);
            }
            finally
            {
                _uow.Dispose();
            }
            return res;
        }
        public async Task<int> Update(SysPlatformEntity model)
        {
            return await _freeSql.Update<SysPlatformEntity>().SetSource(model).ExecuteAffrowsAsync();
        }

        public async Task<PageModel<SysPlatformEntityOutput>> QueryPage(SysPlatformEntityQuery pageQuery)
        {
            var sqlQuery = _freeSql.Select<SysPlatformEntity>()
                               .Where(m => !m.DeleteFlag)
                               .WhereIf(!string.IsNullOrEmpty(pageQuery.NickName), m => m.NickName.Contains(pageQuery.NickName))
                               .OrderByDescending(m => m.CreateTime);

            var totalCount = await sqlQuery.CountAsync();
            var data = await sqlQuery.Page(pageQuery.Current.Value, pageQuery.PageSize.Value).ToListAsync<SysPlatformEntity>();
            return new PageModel<SysPlatformEntityOutput>()
            {
                PageNo = pageQuery.Current.Value,
                PageSize = pageQuery.PageSize.Value,
                TotalCount = totalCount,
                TotalPage = (totalCount + pageQuery.PageSize.Value - 1) / pageQuery.PageSize.Value,
                Data = data.Select(m => new SysPlatformEntityOutput()
                {
                    Id = m.Id,
                    NickName = m.NickName,
                    UUid = m.UUid,
                    LinkUrl = m.LinkUrl,
                    QrCode = m.QrCode,
                    CreateUser = m.CreateUser,
                    CreateTime = m.CreateTime,
                    UpdateUser = m.UpdateUser,
                    UpdateTime = m.UpdateTime,
                    DeleteFlag = m.DeleteFlag,
                }).ToList()
            };
        }

        public async Task<SysPlatformEntity> Get(string id)
        {
            return await _freeSql.Select<SysPlatformEntity>().Where(m => m.Id == id).FirstAsync();
        }

        public async Task<bool> Exists(string uuid)
        {
            return await _freeSql.Select<SysPlatformEntity>().Where(m => m.UUid == uuid && !m.DeleteFlag).AnyAsync();
        }

        public async Task<List<SysPlatformEntity>> GetList()
        {
            return await _freeSql.Select<SysPlatformEntity>().ToListAsync();
        }
    }
}
